<template>
  <el-container class="home_container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img
          src="../assets/heima.png"
          alt=""
        >
        <span>电商管理后台</span>
      </div>
      <el-button
        type="info"
        @click="logout"
      >退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边导航栏 -->
      <el-aside :width="isCollapse?'60px':'200px'">
        <div
          class="toggle"
          @click="isCollapse=!isCollapse"
        >|||</div>
        <!-- 左侧菜单栏 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          :collapse='isCollapse'
          :collapse-transition='false'
          router
          :default-active="activePath"
        >
          <el-submenu
            :index="item.id+''"
            v-for="item in menulist"
            :key="item.id"
          >
            <!-- 一级标题 -->
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span style="margin-left:5px;">{{item.authName}}</span>
            </template>
            <el-menu-item
              :index="'/'+item1.path"
              v-for="item1 in item.children"
              :key="item1.id"
              @click="changeActivePath('/'+item1.path)"
            >
              <!-- 二级标题 -->
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{item1.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧主题内容 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      menulist: [], // 左侧导航栏数据
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  methods: {
    changeActivePath (path) {
      this.activePath = path
      window.sessionStorage.setItem('activePath', path)
    },
    logout () {
      // 清空token
      window.sessionStorage.clear()
      // 跳转到登录页
      this.$router.push('/login')
    },
    async getMenus () {
      const { data: res } = await this.$http.get('menus')
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error('获取数据失败')
      this.menulist = res.data
      // console.log()
      // console.log(this.menulist)
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { // 页面打开就会执行
    this.getMenus()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  mounted () { }
}
</script>

<style scoped lang='less'>
.home_container {
  height: 100%;
  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: #fff;
    font-size: 20px;
    > div {
      display: flex;
      align-items: center;
      span {
        margin-left: 15px;
      }
    }
  }
  .el-aside {
    background-color: #333744;
  }
}
.el-menu {
  border-right: none;
}
.toggle {
  background-color: #4a5064;
  text-align: center;
  color: #fff;
  padding: 4px 0;
}
.el-main {
  background-color: #eaedf1;
}
</style>
